//
//  GoodPriceView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/25.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "GoodPriceView.h"

@interface GoodPriceView ()

// 价格view
@property (strong, nonatomic) UIView *realPriceView;
@property (strong, nonatomic) UILabel *realPriceL;
@property (strong, nonatomic) UILabel *originalPriceL;
@property (strong, nonatomic) UIView *discountView;
@property (strong, nonatomic) UIView *stockDiscountView;
@property (strong, nonatomic) UILabel *discountL;
// 进货详情页冻结期
@property (strong, nonatomic) UIImageView *frozenDateIV;
@property (strong, nonatomic) UILabel *frozenDateL;

//抵扣劵抵扣后价格
@property (strong, nonatomic) UIView *deductiblePriceView;
@property (strong, nonatomic) UILabel *deductiblePriceL;
@property (strong, nonatomic) UILabel *deductibleL;
@property (strong, nonatomic) UILabel *deductibleMoneyL;

@property (strong, nonatomic) UILabel *memberLevel;
@property (strong, nonatomic) UILabel *memberDesL;
@property (strong, nonatomic) UIView *percentView;
@property (strong, nonatomic) UILabel *percentL;
//@property (strong, nonatomic) UIView *lineV;

@property (assign, nonatomic) NSInteger type;


@end

@implementation GoodPriceView

- (instancetype)initType:(NSInteger)type {
    
    if (self = [super init]) {
        
        self.type = type;
        
        [self addSubview:self.realPriceView];
        [self.realPriceView addSubview:self.realPriceL];
        [self.realPriceView addSubview:self.originalPriceL];
        [self.realPriceView addSubview:self.discountView];
        [self.realPriceView addSubview:self.stockDiscountView];
        [self.realPriceView addSubview:self.discountL];
        [self.realPriceView addSubview:self.frozenDateL];
        [self.realPriceView addSubview:self.frozenDateIV];
        
        [self addSubview:self.deductiblePriceView];
        [self.deductiblePriceView addSubview:self.deductiblePriceL];
        [self.deductiblePriceView addSubview:self.deductibleL];
        [self.deductiblePriceView addSubview:self.deductibleMoneyL];
        [self.deductiblePriceView addSubview:self.memberLevel];
        [self.deductiblePriceView addSubview:self.memberDesL];
        [self.deductiblePriceView addSubview:self.percentView];
        [self.deductiblePriceView addSubview:self.percentL];
//        [self addSubview:self.lineV];
        
//
//        if (type == 0) {
//
//            self.realPriceL.textColor = Colorff0000;
//            self.originalPriceL.text = @"￥6800";
//            self.frozenDateIV.hidden = YES;
//            self.frozenDateL.hidden = YES;
//            self.deductibleL.text = @"积分可抵扣";
//            self.deductibleMoneyL.hidden = NO;
//
//            self.stockDiscountView.hidden = YES;
//            self.discountView.hidden = NO;
//
//        }else {
//
//            self.realPriceL.textColor = Color1f89ff;
//            self.originalPriceL.text = @"进货价格";
//            self.frozenDateIV.hidden = NO;
//            self.frozenDateL.hidden = NO;
//            self.deductibleMoneyL.hidden = YES;
//            self.deductibleL.text = @"预期利润";
//
//            self.stockDiscountView.hidden = NO;
//            self.discountView.hidden = YES;
//
//        }
        
    }
    
    return self;
}

- (void)setContentDict:(NSDictionary *)contentDict {
    
    _contentDict = contentDict;
    
    self.memberLevel.hidden = YES;
    self.memberDesL.hidden = YES;
    self.percentL.hidden = YES;
    self.percentView.hidden = YES;
    
    if (self.type == 0) {
        
        self.realPriceL.textColor = Colorff0000;
        self.originalPriceL.text = [NSString stringWithFormat:@"￥%@", contentDict[@"price"]];
       
        NSString *priceStr1 = [NSString stringWithFormat:@"￥%@", contentDict[@"sale_price"]];
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:priceStr1];
        [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
        [attributeStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:jisuan(40)] range:NSMakeRange(1, priceStr1.length - 1)];
        self.realPriceL.attributedText = attributeStr;
        
        self.discountL.text = contentDict[@"sale"];
       
        NSString *priceStr = [NSString stringWithFormat:@"￥%@", contentDict[@"dprice"]];
        NSMutableAttributedString *priceAttribute = [[NSMutableAttributedString alloc] initWithString:priceStr];
        [priceAttribute addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
        [priceAttribute addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:jisuan(30)] range:NSMakeRange(1, priceStr.length - 1)];
        self.deductiblePriceL.attributedText = priceAttribute;
        
        self.deductibleMoneyL.text = [NSString stringWithFormat:@"%@元", contentDict[@"dintegral"]];
        
        self.frozenDateIV.hidden = YES;
        self.frozenDateL.hidden = YES;
        self.deductibleL.text = @"积分可抵扣";
        self.deductibleMoneyL.hidden = NO;
        
        self.stockDiscountView.hidden = YES;
        self.discountView.hidden = NO;
        
    }else {
        
        self.realPriceL.textColor = Color1f89ff;
        self.originalPriceL.text = @"进货价格";
        self.frozenDateIV.hidden = NO;
        self.frozenDateL.hidden = NO;
        self.deductibleMoneyL.hidden = YES;
        self.deductibleL.text = @"预期利润";
        
        self.stockDiscountView.hidden = NO;
        self.discountView.hidden = YES;
        
    }
    
    
    
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.realPriceView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.mas_top);
        make.height.mas_equalTo(jisuan(80));
        
    }];
    [self.realPriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.realPriceView.mas_left).with.offset(jisuan(20));
        make.top.mas_equalTo(weakSelf.realPriceView.mas_top).with.offset(jisuan(30));
        
    }];
    [self.originalPriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.realPriceL.mas_right).with.offset(jisuan(10));
        make.bottom.mas_equalTo(weakSelf.realPriceView.mas_bottom).with.offset(-jisuan(16));
        
    }];
    [self.discountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.realPriceView.mas_bottom).with.offset(-jisuan(16));
        make.left.mas_equalTo(weakSelf.originalPriceL.mas_right).with.offset(jisuan(16));
        make.width.mas_equalTo(jisuan(60));
        make.height.mas_equalTo(jisuan(30));
    }];
    [self.stockDiscountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.realPriceView.mas_bottom).with.offset(-jisuan(16));
        make.left.mas_equalTo(weakSelf.originalPriceL.mas_right).with.offset(jisuan(16));
        make.width.mas_equalTo(jisuan(70));
        make.height.mas_equalTo(jisuan(30));
    }];
    
    [self.discountL mas_makeConstraints:^(MASConstraintMaker *make) {
    
        if (self.type == 0) {
    
            make.centerX.mas_equalTo(weakSelf.discountView.mas_centerX);
            make.centerY.mas_equalTo(weakSelf.discountView.mas_centerY);
            
        }else {
            
            make.centerX.mas_equalTo(weakSelf.stockDiscountView.mas_centerX);
            make.centerY.mas_equalTo(weakSelf.stockDiscountView.mas_centerY);
            
        }
        
    }];
    [self.frozenDateL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.realPriceView.mas_right).with.offset(-jisuan(20));
        make.top.mas_equalTo(weakSelf.realPriceView.mas_top).with.offset(jisuan(40));
        
    }];
    
    [self.frozenDateIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.centerY.mas_equalTo(weakSelf.frozenDateL.mas_centerY);
        make.right.mas_equalTo(weakSelf.frozenDateL.mas_left).with.offset(-jisuan(5));
        make.width.mas_equalTo(jisuan(26));
        make.height.mas_equalTo(jisuan(26));
        
    }];
    
    [self.deductiblePriceView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
        make.top.mas_equalTo(weakSelf.realPriceView.mas_bottom);
        
    }];
    [self.deductiblePriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.deductiblePriceView.mas_left).with.offset(jisuan(20));
        make.bottom.mas_equalTo(weakSelf.deductiblePriceView.mas_bottom).with.offset(-jisuan(25));
        
    }];
    [self.deductibleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.deductiblePriceL.mas_right).with.offset(jisuan(10));
        make.bottom.mas_equalTo(weakSelf.deductiblePriceView.mas_bottom).with.offset(-jisuan(25));
    }];
    [self.deductibleMoneyL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.deductibleL.mas_right);
        make.bottom.mas_equalTo(weakSelf.deductibleL.mas_bottom);
    }];
    
    [self.percentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.deductiblePriceView.mas_right).with.offset(-jisuan(20));
        make.bottom.mas_equalTo(weakSelf.deductiblePriceView.mas_bottom).with.offset(-jisuan(25));
        make.width.mas_equalTo(jisuan(60));
        make.height.mas_equalTo(jisuan(30));
    }];
    [self.percentL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.percentView.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.percentView.mas_centerY);
    }];
    
    if (self.type == 0) {
       
        [self.memberLevel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(weakSelf.percentView.mas_left).with.offset(-jisuan(10));
            make.centerY.mas_equalTo(weakSelf.percentView.mas_centerY);
        }];
        [self.memberDesL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(weakSelf.memberLevel.mas_left);
            make.centerY.mas_equalTo(weakSelf.percentView.mas_centerY);
        }];
        
    }else {
        
        [self.memberDesL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(weakSelf.percentView.mas_left).with.offset(-jisuan(10));
            make.centerY.mas_equalTo(weakSelf.percentView.mas_centerY);
          
        }];
        [self.memberLevel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(weakSelf.memberDesL.mas_left);
            make.centerY.mas_equalTo(weakSelf.percentView.mas_centerY);
    
        }];
     
        
    }
    
//    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.left.mas_equalTo(weakSelf.mas_left);
//        make.right.mas_equalTo(weakSelf.mas_right);
//        make.bottom.mas_equalTo(weakSelf.mas_bottom);
//        make.height.mas_equalTo(jisuan(1));
//    }];
    
}

#pragma mark -lazy
- (UIView *)realPriceView {
    
    if (!_realPriceView) {
        
        _realPriceView = [[UIView alloc] init];
        _realPriceView.backgroundColor = WhiteColor;
    }

    return _realPriceView;
}

- (UILabel *)realPriceL {
    
    if (!_realPriceL) {
        
        _realPriceL = [[UILabel alloc] init];
        _realPriceL.textColor = Colorff0000;
        _realPriceL.textAlignment = NSTextAlignmentLeft;
        
//        NSString *priceStr = @"￥5399";
//        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:priceStr];
//        [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
//        [attributeStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:jisuan(40)] range:NSMakeRange(1, priceStr.length - 1)];
//        _realPriceL.attributedText = attributeStr;
        
    }
    
    return _realPriceL;
}

- (UILabel *)originalPriceL {
    
    if (!_originalPriceL) {
        
        _originalPriceL = [[UILabel alloc] init];
        _originalPriceL.textAlignment = NSTextAlignmentLeft;
        _originalPriceL.textColor = Color999999;
//        _originalPriceL.text = @"￥6800";
        _originalPriceL.font = Font(20);
    }
    
    return _originalPriceL;
}

- (UIView *)discountView {
    
    if (!_discountView) {
        
        _discountView = [[UIView alloc] init];
        _discountView.backgroundColor = Colorff0000;
        _discountView.alpha = 0.2;
        _discountView.layer.cornerRadius = jisuan(15);
        _discountView.layer.masksToBounds = YES;
        
    }
    
    return _discountView;
}
- (UIView *)stockDiscountView {
    
    if (!_stockDiscountView) {
        
        _stockDiscountView = [[UIView alloc] init];
        _stockDiscountView.backgroundColor = Colorff0000;
        _stockDiscountView.alpha = 0.2;
        _stockDiscountView.layer.cornerRadius = jisuan(15);
        _stockDiscountView.layer.masksToBounds = YES;
        
    }
    
    return _stockDiscountView;
}
- (UILabel *)discountL {
    
    if (!_discountL) {
        
        _discountL = [[UILabel alloc] init];
        _discountL.textAlignment = NSTextAlignmentCenter;
        _discountL.textColor = Colorff0000;
        _discountL.text = @"7.9折";
        _discountL.font = Font(20);
    }
    
    return _discountL;
}

- (UILabel *)frozenDateL {
    
    if (!_frozenDateL) {
        
        _frozenDateL = [[UILabel alloc] init];
        _frozenDateL.textAlignment = NSTextAlignmentRight;
        _frozenDateL.textColor = Color999999;
        _frozenDateL.text = @"冻结期：30天";
        _frozenDateL.font = Font(20);
        
    }
    
    return _frozenDateL;
    
}

- (UIImageView *)frozenDateIV {
    
    if (!_frozenDateIV) {
    
        _frozenDateIV = [[UIImageView alloc] init];
        _frozenDateIV.contentMode = UIViewContentModeScaleAspectFit;
        _frozenDateIV.image = [UIImage imageNamed:@"stockContentViewTimeBG"];
        
    }
    
    return _frozenDateIV;
}

- (UIView *)deductiblePriceView {
    
    if (!_deductiblePriceView) {
        
        _deductiblePriceView = [[UIView alloc] init];
        _deductiblePriceView.backgroundColor = WhiteColor;
        
    }
    
    return _deductiblePriceView;
}

- (UILabel *)deductiblePriceL {
    
    if (!_deductiblePriceL) {
        
        _deductiblePriceL = [[UILabel alloc] init];
        _deductiblePriceL.textColor = Colorff0000;
        _deductiblePriceL.textAlignment = NSTextAlignmentLeft;
//
//        NSString *priceStr = @"￥29";
//        NSMutableAttributedString *priceAttribute = [[NSMutableAttributedString alloc] initWithString:priceStr];
//        [priceAttribute addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
//        [priceAttribute addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:jisuan(30)] range:NSMakeRange(1, priceStr.length - 1)];
//        _deductiblePriceL.attributedText = priceAttribute;
//
    }
    
    return _deductiblePriceL;
}

- (UILabel *)deductibleL {
    
    if (!_deductibleL) {
        
        _deductibleL = [[UILabel alloc] init];
        _deductibleL.textAlignment = NSTextAlignmentLeft;
        _deductibleL.textColor = Color999999;
        _deductibleL.font = Font(20);
        _deductibleL.text = @"积分可抵扣";
        
    }
    
    return _deductibleL;
}

- (UILabel *)deductibleMoneyL {
    
    if (!_deductibleMoneyL) {
        
        _deductibleMoneyL = [[UILabel alloc] init];
        _deductibleMoneyL.textColor = Colorff0000;
        _deductibleMoneyL.textAlignment = NSTextAlignmentLeft;
        _deductibleMoneyL.font = Font(20);
//        _deductibleMoneyL.text = @"1620元";
        
    }
    
    return _deductibleMoneyL;
}

- (UILabel *)memberLevel {
    
    if (!_memberLevel) {
        
        _memberLevel = [[UILabel alloc] init];
        _memberLevel.textAlignment = NSTextAlignmentRight;
        _memberLevel.textColor = Color1f89ff;
        _memberLevel.font = Font(20);
        _memberLevel.text = @"V1";
    }
    
    return _memberLevel;
}

- (UILabel *)memberDesL {
    
    if (!_memberDesL) {
        
        _memberDesL = [[UILabel alloc] init];
        _memberDesL.textAlignment = NSTextAlignmentRight;
        _memberDesL.textColor = Color999999;
        _memberDesL.font = Font(20);
        _memberDesL.text = @"会员等级为";
        
    }
    
    return _memberDesL;
    
}

- (UIView *)percentView {
    
    if (!_percentView) {
        
        _percentView = [[UIView alloc] init];
        _percentView.backgroundColor = Colorff0000;
        _percentView.alpha = 0.2;
        _percentView.layer.cornerRadius = jisuan(15);
        _percentView.layer.masksToBounds = YES;
    }
    
    return _percentView;
}

- (UILabel *)percentL {
    
    if (!_percentL) {
        
        _percentL = [[UILabel alloc] init];
        _percentL.textAlignment = NSTextAlignmentRight;
        _percentL.textColor = Colorff0000;
        _percentL.font = Font(20);
        _percentL.text = @"30%";
        
    }
    
    return _percentL;
    
}

//- (UIView *)lineV {
//
//    if (!_lineV) {
//
//        _lineV = [[UIView alloc] init];
//        _lineV.backgroundColor = Colordddddd;
//    }
//
//    return _lineV;
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
